package cn.magicduck.demo.store.impl;

import cn.magicduck.demo.store.ICommodity;
import com.alibaba.fastjson.JSON;
import demo.coupon.CouponResult;
import demo.coupon.CouponService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @projectName: design-pattern-demo
 * @className: CouponCommodityService
 * @description: 模拟发放优惠券服务
 * @author: MagicDuck
 * @create: 2020-07-16 16:27
 **/
public class CouponCommodityService implements ICommodity {

    private Logger logger = LoggerFactory.getLogger(CouponCommodityService.class);

    /** 模拟注入 */
    private CouponService couponService = new CouponService();

    public void sendCommodity(String uId, String commodityId, String bizId, Map<String, String> extMap) throws Exception {
        CouponResult couponResult = couponService.sendCoupon(uId, commodityId, bizId);
        logger.info("请求参数[优惠券] uId：{} commodityId：{} bizId：{} extMap：{}", uId, commodityId, bizId, JSON.toJSON(extMap));
        logger.info("请求结果[优惠券] {}", JSON.toJSON(couponResult));
        if (!"0000".equals(couponResult.getCode())) {
            throw new RuntimeException(couponResult.getInfo());
        }
    }
}
